import i18n from '@/i18n'

// 常量Key前缀
export const SettingPrefix = 'Dishcare.Dishwasher.Setting.LearningDishwasher'
export const StatusPrefix = 'Dishcare.Dishwasher.Status.LearningDishwasher'
export const CommandPrefix = 'Dishcare.Dishwasher.Command.LearningDishwasher'
export const EnumPrefix = 'Dishcare.Dishwasher.EnumType.LearningDishwasher'
export const OptionPrefix = 'Dishcare.Dishwasher.Option.LearningDishwasher'
export const EventPrefix = 'Dishcare.Dishwasher.Event.LearningDishwasher'

// 程序Key
export const LearningProgramKey = 'Dishcare.Dishwasher.Program.LearningDishwasher'

/**
 * Learning 阶段
 */
// 阶段Key
export const LearningPhaseKey = `${StatusPrefix}.Phase`
export enum LearningPhase {
  Initial = `Dishcare.Dishwasher.EnumType.LearningDishwasher.Phase.Initial`,
  Learning = `Dishcare.Dishwasher.EnumType.LearningDishwasher.Phase.Learning`,
  Usage = `Dishcare.Dishwasher.EnumType.LearningDishwasher.Phase.Usage`,
}

/**
 * Learning 维度
 */
export enum LearningDimension {
  Cleaning = 'Cleaning',
  Drying = 'Drying',
  Duration = 'Duration',
}

/**
 * 预设置
 */
export const PresettingCleaning = `${SettingPrefix}.InitialPreference.Cleaning`
export const PresettingDrying = `${SettingPrefix}.InitialPreference.Drying`
export const PresettingDuration = `${SettingPrefix}.InitialPreference.Duration`
export const PresettingKeys = [PresettingCleaning, PresettingDrying, PresettingDuration]

/**
 * Meta 数据
 */
export function getMetaQueryKey(level: LearningLevelKey) {
  return `${OptionPrefix}.${level}`
}

/**
 * 程序方案
 */
// Learning强度
export enum LearningLevelKey {
  Cleaning = 'CleaningLevel',
  Drying = 'DryingLevel',
  Duration = 'DurationLevel',
}
export type LearningLevelValue =
  | 'Dishcare.Dishwasher.EnumType.LearningDishwasher.Level0'
  | 'Dishcare.Dishwasher.EnumType.LearningDishwasher.Level1'
  | 'Dishcare.Dishwasher.EnumType.LearningDishwasher.Level2'
  | 'Dishcare.Dishwasher.EnumType.LearningDishwasher.Level3'

/**
 * 评价反馈
 */
export const FeedbackCleaningKey = `${SettingPrefix}.Feedback.Cleaning`
export const FeedbackDryingKey = `${SettingPrefix}.Feedback.Drying`
export const FeedbackDurationKey = `${SettingPrefix}.Feedback.Duration`
export enum FeedbackValue {
  VeryDissatisfied = `Dishcare.Dishwasher.EnumType.LearningDishwasher.Feedback.VeryDissatisfied`,
  Dissatisfied = `Dishcare.Dishwasher.EnumType.LearningDishwasher.Feedback.Dissatisfied`,
  Satisfied = `Dishcare.Dishwasher.EnumType.LearningDishwasher.Feedback.Satisfied`,
  VerySatisfied = `Dishcare.Dishwasher.EnumType.LearningDishwasher.Feedback.VerySatisfied`,
}
// 评价反馈事件
// 事件Key
export const FeedbackRequested = `${EventPrefix}.Feedback.Requested`
export const FeedbackRequestedStillSatisfied = `${EventPrefix}.Feedback.RequestedStillSatisfied`
// 事件Value

/**
 * 程序方案调整
 *  - serialize开头的方法 用于判断后端的Key值 或传给后端
 *  - parse开头的方法 用于解析后端的Key值
 */
export enum ProposalType {
  // 之前方案
  Current = 'Current',
  // 优化方案
  Main = 'Main',
  // 省时方案
  Speed = 'Speed',
  // 节能方案
  Eco = 'Eco',
}
// 之前方案前缀
export const CurrentProposalPrefix = `${StatusPrefix}.Current`
// 其他方案前缀
export const CustomProposalPrefix = `${StatusPrefix}.Proposal`

// 序列化 Proposal Key
export function serializeProposalKey(proposal: ProposalType, level: LearningLevelKey): string {
  if (proposal === ProposalType.Current) {
    return `${CurrentProposalPrefix}.${level}`
  }

  return `${CustomProposalPrefix}.${proposal}.${level}`
}
// 解析之前方案的 Proposal Level
export function parseCurrentProposalLevel(key: string): LearningLevelKey {
  return key.replace(`${CurrentProposalPrefix}.`, '') as LearningLevelKey
}
// 解析其他方案的 ProposalType 和 Level
export function parseCustomProposalInfo(key: string): { type: ProposalType; level: LearningLevelKey } {
  const [type, level] = key.replace(`${CustomProposalPrefix}.`, '').split('.') as [ProposalType, LearningLevelKey]
  return { type, level }
}
/**
 * 获取当前 ProposalType 下所有LevelKey
 */
export function getAllDimensionLevelKeys(type: ProposalType) {
  return [
    {
      dimension: LearningDimension.Cleaning,
      type: LearningLevelKey.Cleaning,
      key: serializeProposalKey(type, LearningLevelKey.Cleaning),
    },
    {
      dimension: LearningDimension.Drying,
      type: LearningLevelKey.Drying,
      key: serializeProposalKey(type, LearningLevelKey.Drying),
    },
    {
      dimension: LearningDimension.Duration,
      type: LearningLevelKey.Duration,
      key: serializeProposalKey(type, LearningLevelKey.Duration),
    },
  ]
}

/**
 * 程序方案调整 确定
 */
export const ProposalCalculationFinished = `${StatusPrefix}.Proposal.CalculationFinished`
export function getAcceptProposalKey(type: ProposalType) {
  return `${CommandPrefix}.Proposal.${type}.Accept`
}

/**
 * 程序不可调整
 */
export const NotImprovedSuffixes: Record<LearningDimension, string> = {
  [LearningDimension.Cleaning]: 'CleaningCannotBeImproved',
  [LearningDimension.Drying]: 'DryingCannotBeImproved',
  [LearningDimension.Duration]: 'SpeedCannotBeImproved',
}

export function getNotImprovedKey(type: ProposalType, dimension: LearningDimension) {
  const notImprovedSuffix = NotImprovedSuffixes[dimension]
  return `${StatusPrefix}.Proposal.${type}.${notImprovedSuffix}`
}
export function getNotImprovedMap(type: ProposalType) {
  return [
    {
      key: getNotImprovedKey(type, LearningDimension.Cleaning),
      dimension: LearningDimension.Cleaning,
      label: i18n.global.t('Appliance.LearningProgram.Meta.Cleaning.Text'),
    },
    {
      key: getNotImprovedKey(type, LearningDimension.Drying),
      dimension: LearningDimension.Drying,
      label: i18n.global.t('Appliance.LearningProgram.Meta.Drying.Text'),
    },
    {
      key: getNotImprovedKey(type, LearningDimension.Duration),
      dimension: LearningDimension.Duration,
      label: i18n.global.t('Appliance.LearningProgram.Meta.Duration.Text'),
    },
  ]
}
